package org.gnomus.server;

import org.gnomus.util.strings;

import java.util.HashMap;

public class Cookie extends HashMap<String, String> {
    private Integer ma = 0;
    private HashMap<String, String> original = null;


    public Cookie(Context ctx) {
        javax.servlet.http.Cookie[] cookies = ctx.req.getCookies();
        if (cookies != null) {
            for (javax.servlet.http.Cookie c : cookies) {
                if ("gnomus".equals(c.getName())) {
//                    putAll(strings.toMap("{hello=world, sha1user=9577c1cc290bec9baca1fb05e5de81592c804a58}"));
                    original = new HashMap<String, String>(strings.toMap(c.getValue()));
                    putAll(strings.toMap(c.getValue()));
                }
            }
        }
    }

    public javax.servlet.http.Cookie getCookie() {
        javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie("gnomus", this.toString());
        cookie.setPath("/");
        if (ma > 0) {
            cookie.setMaxAge(ma);
        }
        return cookie;
    }

    public void setMaxAge(int age) {
        ma = age;
    }

    public boolean isDirty() {
        return !equals(original);
    }
}
